package com.javalong.mir.dwz.controller;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.javalong.mir.common.util.ImageUtils;
import com.javalong.mir.common.util.QiNiuUtils;
import com.javalong.mir.constant.Constant;
import com.javalong.mir.constant.StatusCode;
import com.javalong.mir.controller.ImageController;
import com.javalong.mir.dwz.vo.DwzResponse;
import com.javalong.mir.pojo.Media;
import com.javalong.mir.service.MediaService;

@Controller
@RequestMapping("dwz/media")
public class DwzMediaController {
	
	@Autowired
	private MediaService mediaService;
	
	@RequestMapping("list")
	public String list(Model model) {
		List<Media> mediaList = mediaService.getList(0, Constant.PAGE_SIZE);
		model.addAttribute(mediaList);
		model.addAttribute("totalCount", mediaService.getCount());
		model.addAttribute("currentPage", 1);
		model.addAttribute("numPerPage", Constant.PAGE_SIZE);
		return "media/list";
	}

	@RequestMapping("listByPage")
	public String listByPage(@RequestParam("pageNum") int pageNum, @RequestParam("numPerPage") int numPerPage,
			Model model) {
		List<Media> mediaList = mediaService.getMediaList(pageNum - 1, numPerPage);
		model.addAttribute(mediaList);
		model.addAttribute("totalCount", mediaService.getCount());
		model.addAttribute("currentPage", pageNum);
		model.addAttribute("numPerPage", numPerPage);
		return "media/list";
	}

	@RequestMapping("delete")
	@ResponseBody
	public DwzResponse delete(@RequestParam("id") int id) {
		mediaService.delete(id);
		DwzResponse response = new DwzResponse();
		response.setStatusCode(StatusCode.SUCCESS);
		response.setMessage("删除成功!");
		response.setNavTabId("media_list");
		return response;
	}

	@RequestMapping("addjsp")
	public String addjsp() {
		return "media/add";
	}
	
	@RequestMapping("addmorejsp")
	public String addmorejsp() {
		return "media/addmore";
	}

	@RequestMapping("addmore")
	public 	DwzResponse addmore(@RequestParam(value = "file")List<MultipartFile> files) {
		for(int i=0;i<files.size();i++){
			MultipartFile file = files.get(i);
			try {
				int height = ImageUtils.getImgHeight(file.getInputStream());
				int width = ImageUtils.getImgWidth(file.getInputStream());
				Media media = new Media();
				media.setImageHeight(height);
				media.setImageWidth(width);
				media.setInfo("");
				media.setTitle("");
				String url = QiNiuUtils.upload(file.getBytes());
				media.setAvatarUrl(ImageController.QINIU_PREFIX+url);
				mediaService.save(media);
				System.out.println(media.toString());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		DwzResponse response = new DwzResponse();
		response.setStatusCode(StatusCode.SUCCESS);
		response.setMessage("添加成功!");
		response.setNavTabId("media_list");
		response.setCallbackType("closeCurrent");
		return response;
	}

	
	
	@RequestMapping("add")
	@ResponseBody
	public DwzResponse add(Media media) {
 		mediaService.save(media);
		DwzResponse response = new DwzResponse();
		response.setStatusCode(StatusCode.SUCCESS);
		response.setMessage("添加成功!");
		response.setNavTabId("media_list");
		response.setCallbackType("closeCurrent");
		return response;
	}

	@RequestMapping("updatejsp")
	public String updatejsp(@RequestParam("id")int id,Model model) {
		Media media = mediaService.findBannerById(id);
		if(media!=null){
			model.addAttribute(media);
		}
		return "media/update";
	}

	@RequestMapping("update")
	@ResponseBody
	public DwzResponse update(Media media) {
		mediaService.updateMedia(media);
		DwzResponse response = new DwzResponse();
		response.setStatusCode(StatusCode.SUCCESS);
		response.setMessage("更新成功!");
		response.setNavTabId("media_list");
		response.setCallbackType("closeCurrent");
		return response;
	}


}

